<?php

namespace App\Service\Index;

use App\Constants\ErrorCode;
use App\Model\CallBase;
use App\Model\CallComment;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\BaseService;

/**
 * 话聊详情
 */
class VoiceCallDetailService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'channel_id' => 'required|string'
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $appId = $this->request->input('appid', 1);
        $version = $this->request->input('version', '');
        //频道ID
        $channelId = $this->request->input('channel_id', 0);
        //通话类型
        $callType = $this->request->input('call_type', 0);
        //获取话单
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;
        $callInfo = $callCls::getInfoByChannel($channelId);
        if (!$callInfo || !in_array($userId, [$callInfo->chat_user_id, $callInfo->user_id])) {
            log_info("==wrong_call==" . json_encode($callInfo));
            return $this->setError(ErrorCode::PARAMETER_ERROR, '该话单信息错误，请咨询客服查询');
        }

        $caller = $userId == $callInfo->user_id;
        if ($caller) {
            $callRole = 1;
            $chatUserId = $callInfo->chat_user_id;
        } else {
            $callRole = 2;
            $chatUserId = $callInfo->user_id;
        }

        if ($callInfo->from == CallBase::FROM_INVITE_CALL){
            $caller = $userId == $callInfo->user_id;
            if ($caller) {
                $callRole = 2;
                $chatUserId = $callInfo->chat_user_id;
            } else {
                $callRole = 1;
                $chatUserId = $callInfo->user_id;
            }
        }
        $userInfo = UserProfile::getProfile($chatUserId);
        $chatNickName = $userInfo->nick_name;
        $chatAvatar = $userInfo->avatar;

        $ret = [
            'user_id' => $userId,
            'call_role' => $callRole,
            'call_id' => $callInfo->id,
            'channel_id' => $callInfo->channel_id,
            'chat_user_id' => $chatUserId,
            'chat_nick_name' => $chatNickName,
            'chat_avatar' => $chatAvatar,
            'cost_coin' => "{$callInfo->caller_cost}金币",
            'gain_coin' => "{$callInfo->host_gain}金币",
            'call_duration' => ceil($callInfo->call_time / 60) . "分钟",
            'start_time' => date('Y-m-d H:i:s', $callInfo->answer_time),
            'end_time' => date('Y-m-d H:i:s', $callInfo->end_time),
            'call_type' => $callType,
        ];
        //评价细节
        $this->packCommentDetail($ret, $callInfo->channel_id, $appId, $version);
        log_user_result($userId, "{$chatUserId}==" . json_encode($ret));
        return $ret;
    }

    /**
     * 话单评价
     */
    function packCommentDetail(&$ret = [], $channelId = 0, $appId = 1, $version = '')
    {
        if (!User::checkValidVersion($version, $appId, ['1.2.7'])) return;
        $comment = CallComment::getDetailByChannel($channelId);
        if (!$comment) $ret['comment_detail'] = null;
        else {
            $ret['comment_detail'] = [
                'comment_level' => [
                    'level' => $comment['level'],
                    'text' => CallComment::LEVEL[$comment['level']],
                ],
                'comment_desc' => $comment['desc'],
                'comment_tag' => $comment['tags']
            ];
        }
        $userProfile = UserProfile::getProfile(($ret['user_id']));
        $ret['can_comment'] = $userProfile->gender == 1 ? 1 : 0;
        return;
    }
}
